package com.linkvin.ec.hcs.service.mdm.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;
import com.linkvin.ec.hcs.dao.mapper.mdm.VSeriesMapper;
import com.linkvin.ec.hcs.dao.model.mdm.VBrand;
import com.linkvin.ec.hcs.dao.model.mdm.VSeries;
import com.linkvin.ec.hcs.dao.model.mdm.bo.VModelBo;
import com.linkvin.ec.hcs.service.mdm.VSeriesService;
import com.linkvin.ec.hcs.service.util.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class VSeriesServiceImpl implements VSeriesService {

    @Autowired
    private VSeriesMapper vSeriesMapper;

    @Override
    public List<VSeries> listAllSeriesByBrandId(Integer brandId) {
        EntityWrapper<VSeries> ew =new EntityWrapper<>();
        ew.eq("brand_id",brandId);
        return vSeriesMapper.selectList(ew);
    }

    @Override
    public List<VSeries> listAllSeriesBySeriesId(Integer seriesId) {
        VSeries vSeries = vSeriesMapper.selectById(seriesId);
        EntityWrapper<VSeries> ew =new EntityWrapper<>();
        ew.eq("brand_id",vSeries.getBrandId());
        return vSeriesMapper.selectList(ew);
    }

    @Override
    public VModelBo addSeries(VModelBo vModelBo) {
        //非空校验
        if (Strings.isNullOrEmpty(vModelBo.getSeriesName().trim())) throw new BaseException("车系名称不能为空");
        if (Objects.equals(vModelBo.getBrandId(),null)) throw new BaseException("所属品牌不能为空");
        //判断是否重复
        EntityWrapper<VSeries> ew = new EntityWrapper<>();
        ew.where("series_name={0}",vModelBo.getSeriesName().trim());
        int count = vSeriesMapper.selectCount(ew);
        if(count > 0){
            throw new BaseException("车系名称重复");
        }
        VSeries vSeries = new VSeries();
        vSeries.setBrandId(vModelBo.getBrandId());
        vSeries.setSeriesName(vModelBo.getSeriesName());
        vSeries.setSeriesClass(vModelBo.getSeriesClass());
        vSeries.setComment(vModelBo.getSeriesComment());
        count = vSeriesMapper.insert(vSeries);
        if(count > 0){
            vModelBo = getSeriesBO(vSeries.getId());
        }
        return  vModelBo;
    }

    @Override
    public VModelBo updateSeries(VModelBo vModelBo) {
        //非空校验
        if (Strings.isNullOrEmpty(vModelBo.getSeriesName().trim())) throw new BaseException("车系名称不能为空");
        if (Objects.equals(vModelBo.getBrandId(),null)) throw new BaseException("所属品牌不能为空");
        //判断是否重复
        EntityWrapper<VSeries> ew = new EntityWrapper<>();
        ew.where("id != {0}",vModelBo.getId()).and("brand_id = {0}",vModelBo.getBrandId()).and("series_name={0}",vModelBo.getSeriesName().trim());
        int count = vSeriesMapper.selectCount(ew);
        if(count > 0){
            throw new BaseException("车系名称重复");
        }
        VSeries vSeries = new VSeries();
        vSeries.setId(vModelBo.getSeriesId());
        vSeries.setBrandId(vModelBo.getBrandId());
        vSeries.setSeriesName(vModelBo.getSeriesName());
        vSeries.setSeriesClass(vModelBo.getSeriesClass());
        vSeries.setComment(vModelBo.getSeriesComment());
        count = vSeriesMapper.updateById(vSeries);
        if(count > 0){
            vModelBo = getSeriesBO(vSeries.getId());
        }
        return  vModelBo;
    }

    @Override
    public int deleteSeries(List<Integer> vModelBoIds) {
        //TODO 需要判断是否已经使用
        return vSeriesMapper.deleteBatchIds(vModelBoIds);
    }

    @Override
    public int getBrandIdBySeriesId(Integer seriesId) {
        int brandId=0;
        VSeries vSeries = vSeriesMapper.selectById(seriesId);
        if(!Objects.equals(vSeries,null)){
            brandId=vSeries.getBrandId();
        }
        return brandId;
    }

    @Override
    public List<VModelBo> queryPageVSeries(Page<VModelBo> vModelBoPage) {
        List<VModelBo> listVehicleBo = vSeriesMapper.queryPageVSeries(vModelBoPage,buildCondition(vModelBoPage.getCondition()));
        return  listVehicleBo;
    }

    private EntityWrapper<VModelBo> buildCondition(Map<String,Object> condition){
        EntityWrapper<VModelBo> ew = new EntityWrapper<>();
        if(!Objects.equals(condition,null)) {
            for (Map.Entry<String, Object> entry : condition.entrySet()) {
                if (entry.getKey().equals("name")) {
                    ew.like("brand_name", entry.getValue().toString());
                }
                if(entry.getKey().equals("id")){
                    ew.eq("brand_id", entry.getValue().toString());
                }
            }
        }
        return ew;
    }

    private VModelBo getSeriesBO(Integer id){
        VModelBo vModelBo = new VModelBo();
        List<VModelBo> listSeries= vSeriesMapper.querySeriesByID(id);
        if(!CollectionUtils.isEmpty(listSeries)){
            vModelBo = listSeries.get(0);
        }
        return vModelBo;
    }
}
